import os
import numpy as np
import pandas as pd
import zipfile

def str2np(x):
    return np.array(x.split(';')).astype(np.float)

def np2str(arr):
    return ';'.join(['%.5f'%x for x in arr])

# rawdata_root = '/media/gserver/data/FashionAI'

# test_root = os.path.join(rawdata_root, 'rank')
# test_pd = pd.read_csv(os.path.join(rawdata_root, 'rank/Tests/question.csv'),
#                        header=None, names=['ImageName', 'AttrKey', 'AttrValues'])
#
# test_pd = test_pd.drop('AttrValues',axis=1)

mode = 'val'


model_name = 'test'
merge_list = [
    'res101-[1234]-0.9660-[0567]-0.9222-aug2.csv'
]


if mode == 'online':
    csv_root = './online_to_merge/'
else:
    csv_root = './val_pred2/csv'


file_path = os.path.join(csv_root, merge_list[0])
merged_pd = pd.read_csv(file_path,header=None, names=['ImageName', 'AttrKey', 'AttrValueProbs'])
merged_pd['AttrValueProbs'] = merged_pd['AttrValueProbs'].apply(str2np)

for file_name in merge_list[1:]:
    file_path = os.path.join(csv_root, file_name)
    part_pd = pd.read_csv(file_path,header=None, names=['ImageName', 'AttrKey', 'AttrValueProbs'])
    merged_pd['AttrValueProbs'] += part_pd['AttrValueProbs'].apply(str2np)

merged_pd['AttrValueProbs'] = merged_pd['AttrValueProbs'] / len(merge_list)
merged_pd['AttrValueProbs'] = merged_pd['AttrValueProbs'].apply(np2str)


print merged_pd.head()
print merged_pd.info()


# make zip file
if mode == 'online':
    merged_pd[['ImageName', 'AttrKey', 'AttrValueProbs']].to_csv('online_pred1b/csv/%s.csv' % model_name, header=None,
                                                               index=False)

    z = zipfile.ZipFile('./online_pred1b/subs_zip/%s.zip'%model_name, 'w', zipfile.ZIP_DEFLATED)
    z.write('./online_pred1b/csv/%s.csv' % model_name, arcname='%s.csv' % model_name)
    z.close()

else:
    merged_pd[['ImageName', 'AttrKey', 'AttrValueProbs']].to_csv('val_pred/csv/%s.csv' % model_name, header=None,
                                                               index=False)
    # todo ADD MAP EVAL WHEN CONCAT VAL

